//
//  FeedbackViewController.swift
//  Wmsmobile
//
//  Created by wangshuning on 16/7/27.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit

import MMDrawerController
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}

fileprivate func <= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l <= r
  default:
    return !(rhs < lhs)
  }
}


class FeedbackViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

    var nav = UINavigationBar()
    var lbutton = UIButton()
    var label = UILabel()
    var imageView = UIImageView()   
    var button = UIButton()
    var drawerController: MMDrawerController?
    //#MARK: – Lifecycle
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.hexStringToUIColor("#eeeeee")
        nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
        UINavigationBar.appearance().barTintColor = UIColor.hexStringToUIColor("#00acff")
        lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
        lbutton.setImage(UIImage(named: "返回"), for: UIControlState())
        //        lbutton.backgroundColor = UIColor.greenColor()
        lbutton.imageView?.contentMode = UIViewContentMode.scaleAspectFill
        lbutton.addTarget(self, action: #selector(FeedbackViewController.didClickButton), for: UIControlEvents.touchUpInside)
        label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
        label.text = "意见反馈"
        //        label.backgroundColor = UIColor.redColor()
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 17)
        
        self.view.addSubview(nav)
        self.view.addSubview(lbutton)
        self.view.addSubview(label)        // Do any additional setup after loading the view.
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(FeedbackViewController.hideKeyboard))
        self.view.addGestureRecognizer(gestureRecognizer)
        self.setUpUI()
      
    }

    func hideKeyboard() {
        self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
        nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
        lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
        label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
        self.view.addSubview(nav)
        self.view.addSubview(lbutton)
        self.view.addSubview(label)
        self.view.addSubview(loginBt)
        stextFiled.resignFirstResponder()
        phoneTextFiled.resignFirstResponder()
    }
    
//    lazy private var uLabel: UILabel = {
//        let label = UILabel()
//        label.text = "意见与反馈"
//        label.backgroundColor = UIColor.hexStringToUIColor("#eeeeee")
//        label.textAlignment = .Center
//        label.font = UIFont.systemFontOfSize(17)
////        label.backgroundColor = UIColor.whiteColor()
//        label.numberOfLines = 0
//        return label
//    }()
    lazy fileprivate var stextFiled: UITextView = {
        let tf = UITextView()
//        tf.textInputView = UIView()
        tf.text = "请输入您的建议"
        tf.delegate = self
        tf.backgroundColor = UIColor.white
        tf.font = UIFont.systemFont(ofSize: 17)
        tf.textColor = UIColor.hexStringToUIColor("#CCCCCC")
        return tf
    }()
    lazy fileprivate var phoneTextFiled: UITextField = {
        let tf = UITextField()
        tf.delegate = self
        tf.placeholder = "请输入联系方式"
        tf.backgroundColor = UIColor.white
        tf.font = UIFont.systemFont(ofSize: 17)
        tf.textColor = UIColor.hexStringToUIColor("#666666")
        return tf
    }()
    lazy fileprivate var loginBt: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "矩形-2"), for: UIControlState())
        button.setTitle("提交反馈", for: UIControlState())
        button.setTitleColor(UIColor.white, for: UIControlState())
        button.addTarget(self, action: #selector(FeedbackViewController.feedAction), for: UIControlEvents.touchUpInside)
//        button.setImage(UIImage(named: "矩形-2"), forState: UIControlState.Normal)
        return button
    }()
    
    
    func setUpUI(){
        self.view.addSubview(self.stextFiled)
        self.view.addSubview(self.phoneTextFiled)
//        self.view.addSubview(self.uLabel)
        self.view.addSubview(self.loginBt)
//        uLabel.snp.makeConstraints { (make) in
//            make.centerX.equalTo(self.view)
//            make.top.equalTo(self.view).offset(94)
//            make.width.equalTo(KscreenWidth)
//            make.height.equalTo(40)
//        }
        stextFiled.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(87)
            make.width.equalTo(KscreenWidth)
            make.height.equalTo(150)
        }
        phoneTextFiled.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.stextFiled).offset(174)
            make.width.equalTo(KscreenWidth)
            make.height.equalTo(44)
        }
        loginBt.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(120)
            make.width.equalTo(KscreenWidth-30)
            make.height.equalTo(50)
        }
    }
    
    
    
    //#MARK: – IBAtions
    func didClickButton(){
        //设置中间视图
        let app = UIApplication.shared
        let app2 = app.delegate;
        let  yunCVC = YunCangViewController()
        let  centerNVC = YunCenterViewController(rootViewController: yunCVC)
        //设置左边抽屉视图
        let leftVC = YunLeftTableViewController()
        //对抽屉效果进行设置
        drawerController = MMDrawerController.init(center: centerNVC, leftDrawerViewController: leftVC)
        drawerController?.showsShadow = false
        drawerController?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.all
        drawerController?.maximumLeftDrawerWidth = 270
        app2?.window!!.rootViewController = drawerController
        
    }
    
    func feedAction() {
        if stextFiled.text=="请输入您的建议"&&phoneTextFiled.text?.characters.count>0{
            
            let alertController = UIAlertController(title: "", message: "请填写意见或联系方式", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
            }))
            self.present(alertController, animated: true, completion: nil)
            self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
            nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
            lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
            label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
            self.view.addSubview(nav)
            self.view.addSubview(lbutton)
            self.view.addSubview(label)
        }else if stextFiled.text=="请输入您的建议"&&phoneTextFiled.text?.characters.count<=0{
            
            let alertController = UIAlertController(title: "", message: "请填写意见或联系方式", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
            }))
            self.present(alertController, animated: true, completion: nil)
            self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
            nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
            lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
            label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
            self.view.addSubview(nav)
            self.view.addSubview(lbutton)
            self.view.addSubview(label)
        }else if stextFiled.text.characters.count>0&&phoneTextFiled.text?.characters.count<=0{
            let alertController = UIAlertController(title: "", message: "请填写意见或联系方式", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
            }))
            self.present(alertController, animated: true, completion: nil)
            self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
            nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
            lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
            label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
            self.view.addSubview(nav)
            self.view.addSubview(lbutton)
            self.view.addSubview(label)
        }else{
            let alertController = UIAlertController(title: "", message: "意见反馈提交成功", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
                self.didClickButton()
                self.stextFiled.text = ""
                self.phoneTextFiled.text = ""
                
            }))
            self.present(alertController, animated: true, completion: nil)
            
            self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
            nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
            lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
            label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
            self.view.addSubview(nav)
            self.view.addSubview(lbutton)
            self.view.addSubview(label)
        }
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        UIView.animate(withDuration: 0.25, animations: {
            self.view.frame = CGRect(x: 0, y: -120, width: KscreenWidth, height: KscreenHeight)
            self.nav.frame = CGRect(x: 0, y: 32, width: KscreenWidth, height: 64)
            self.lbutton.frame = CGRect(x: 15, y: 52, width: 44, height: 44)
            self.label.frame = CGRect(x: 50, y: 52, width: KscreenWidth-120, height: 44)
            self.stextFiled.addSubview(self.nav)
            self.stextFiled.addSubview(self.lbutton)
            self.stextFiled.addSubview(self.label)
        }, completion: { (finished) in
            
        }) 
        return true
    }
    
    
//    func textFieldDidEndEditing(textField: UITextField) {
//        UIView.animateWithDuration(0.25, animations: {
//            self.loginBt.snp.makeConstraints { (make) in
//                make.centerX.equalTo(self.view)
//                make.centerY.equalTo(self.view).offset(120)
//                make.width.equalTo(KscreenWidth-30)
//                make.height.equalTo(50)
//            }
//            
//            }) { (finished) in
//                
//        }
//    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool{
        self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
        self.view.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight)
        nav.frame = CGRect(x: 0, y: 0, width: KscreenWidth, height: 64)
        lbutton.frame = CGRect(x: 15, y: 20, width: 44, height: 44)
        label.frame = CGRect(x: 50, y: 20, width: KscreenWidth-120, height: 44)
        self.view.addSubview(nav)
        self.view.addSubview(lbutton)
        self.view.addSubview(label)
        stextFiled.resignFirstResponder()
        phoneTextFiled.resignFirstResponder()
        return true
    }
    
    
    func textViewDidBeginEditing(_ textView: UITextView){
        if stextFiled.text == "请输入您的建议"{
            stextFiled.text = ""
            stextFiled.textColor = UIColor.hexStringToUIColor("#666666")
        }
    }

   func textViewDidEndEditing(_ textView: UITextView){
    if stextFiled.text.characters.count < 1 {
        stextFiled.text = "请输入您的建议"
        stextFiled.textColor = UIColor.hexStringToUIColor("#CCCCCC")
        stextFiled.resignFirstResponder()
    }else{
       stextFiled.textColor = UIColor.hexStringToUIColor("#666666")
    }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
